package com.yc.bbs.controller;

import com.yc.bbs.dao.AdvertisementMapper;
import com.yc.bbs.entity.Advertisement;
import com.yc.bbs.entity.Result;
import com.yc.bbs.entity.User;
import com.yc.bbs.service.IAdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.http.HttpResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csl
 * @since 2024-12-07
 */
@RestController
@RequestMapping("/advertisement")
public class AdvertisementController {
    @Autowired
    AdvertisementMapper advertisementMapper;
    @Autowired
    IAdvertisementService advertisementService;
    @GetMapping("selectAdvertisement")
    public List<Advertisement> selectAdvertisement(HttpServletResponse resp) throws IOException {
        List<Advertisement> list = advertisementMapper.selectAdvertisement();
//        for (Advertisement advertisement : list) {
//            byte[] image = advertisement.getImage();
//            if (image != null) {
//                // 设置内容长度
//                resp.setContentLength(image.length);
//                // 获取响应的输出流
//                ServletOutputStream out = resp.getOutputStream();
//                // 写入图片字节到输出流
//                out.write(image);
//                // 刷新输出流
//                out.flush();
//            }
//        }
        return list;
    }
// @RequestMapping(value = "updateUserInfoAdmin",produces = "text/html;charset=utf8")
@RequestMapping("updateUserInfoAdmin")
public Result updateUserInfoAdmin(Advertisement updateAdv,HttpSession session) throws IOException {
        Object head =  session.getAttribute("head");
        if(head!=null){
            updateAdv.setImage((byte[]) head);
        }
    // 获取当前时间
    LocalDateTime now = LocalDateTime.now();
    updateAdv.setUpdatedTime(now);
        int res=advertisementService.updateUserInfoAdmin(updateAdv);
        if(res>0){
            return new Result(200,"修改成功",null);
        }
        return new Result(500,"修改有误",null);
    }
    @PostMapping("upload")
    @ResponseBody
    public Result upload(@RequestParam("head") MultipartFile file, HttpSession session) throws IOException {
        byte[] head = file.getBytes();
        if(head.length>0){
            session.setAttribute("head",head);
            return new Result(200,"上传文件成功",head);
        }
        return new Result(500,"上传失败",null);
    }
    @RequestMapping("delAdv")
    public Result delAdv(Integer id,Integer status){
        status=status==0?1:0;
        Advertisement advertisement = advertisementMapper.selectById(id);
        advertisement.setStatus(status);
        int i = advertisementMapper.updateById(advertisement);
        if(i>0){
            return new Result(200,"禁用成功",null);
        }
        return new Result(500,"禁用失败失败",null);
    }
    @RequestMapping("addAdrInfo")
    public Result addAdrInfo(Advertisement advertisement,HttpSession session) throws IOException {
        advertisement.setImage((byte[]) session.getAttribute("addHead"));
        int insert = advertisementMapper.insert(advertisement);
        if(insert>0){
            return new Result(200,"添加成功",null);
        }
        return new Result(500,"添加失败",null);
    }
    @PostMapping("addupload")
    @ResponseBody
    public Result addupload(@RequestParam("head") MultipartFile file, HttpSession session) throws IOException {
        byte[] head = file.getBytes();
        if(head.length>0){
            session.setAttribute("addHead",head);
            return new Result(200,"上传文件成功",head);
        }
        return new Result(500,"上传失败",null);
    }
}
